home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 1995 August / PC Plus Super CD (Issue 106) (PCP106) (August 1995).iso / pcplus / handson / vbwkshp / ini.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1995-05-01  |  11.2 KB  |  214 lines

  1. VERSION 2.00
  2. Begin Form frmini 
  3.    BackColor       =   &H00C0C0C0&
  4.    BorderStyle     =   1  'Fixed Single
  5.    Caption         =   ".INI File Read/Write"
  6.    ClientHeight    =   2100
  7.    ClientLeft      =   3240
  8.    ClientTop       =   3165
  9.    ClientWidth     =   4215
  10.    ControlBox      =   0   'False
  11.    Height          =   2790
  12.    Icon            =   INI.FRX:0000
  13.    Left            =   3180
  14.    LinkTopic       =   "Form1"
  15.    MaxButton       =   0   'False
  16.    MinButton       =   0   'False
  17.    ScaleHeight     =   2100
  18.    ScaleWidth      =   4215
  19.    Top             =   2535
  20.    Width           =   4335
  21.    Begin CommandButton cmdExplain 
  22.       Caption         =   "?"
  23.       FontBold        =   -1  'True
  24.       FontItalic      =   0   'False
  25.       FontName        =   "MS Sans Serif"
  26.       FontSize        =   13.5
  27.       FontStrikethru  =   0   'False
  28.       FontUnderline   =   0   'False
  29.       Height          =   420
  30.       Left            =   3195
  31.       TabIndex        =   5
  32.       Top             =   1575
  33.       Width           =   780
  34.    End
  35.    Begin Timer Timer1 
  36.       Interval        =   1000
  37.       Left            =   3735
  38.       Top             =   1575
  39.    End
  40.    Begin CommandButton cmdQuit 
  41.       Caption         =   "&Quit"
  42.       Height          =   420
  43.       Left            =   2385
  44.       TabIndex        =   2
  45.       Top             =   1575
  46.       Width           =   780
  47.    End
  48.    Begin Shape Shape1 
  49.       Height          =   510
  50.       Left            =   225
  51.       Top             =   1530
  52.       Width           =   2085
  53.    End
  54.    Begin Label Label3 
  55.       Alignment       =   2  'Center
  56.       BackStyle       =   0  'Transparent
  57.       Caption         =   "Drag the window somewhere, quit, then run the demo again to see the changes take effect.  (Note the new coordinates after the drag)"
  58.       ForeColor       =   &H00000000&
  59.       Height          =   600
  60.       Left            =   90
  61.       TabIndex        =   4
  62.       Top             =   855
  63.       Width           =   4110
  64.    End
  65.    Begin Label Label5 
  66.       Alignment       =   2  'Center
  67.       BackStyle       =   0  'Transparent
  68.       Caption         =   "This window position is set according to values in the ini.ini file which are read on loading and, depending on the state of the Options menu, (which is an .ini item also), written on quitting."
  69.       ForeColor       =   &H00000000&
  70.       Height          =   825
  71.       Left            =   90
  72.       TabIndex        =   3
  73.       Top             =   45
  74.       Width           =   4110
  75.    End
  76.    Begin Label Label2 
  77.       BackStyle       =   0  'Transparent
  78.       Caption         =   "Window Left:"
  79.       ForeColor       =   &H000000FF&
  80.       Height          =   195
  81.       Left            =   270
  82.       TabIndex        =   1
  83.       Top             =   1800
  84.       Width           =   1905
  85.    End
  86.    Begin Label Label1 
  87.       BackStyle       =   0  'Transparent
  88.       Caption         =   "Window Top:"
  89.       ForeColor       =   &H000000FF&
  90.       Height          =   195
  91.       Left            =   270
  92.       TabIndex        =   0
  93.       Top             =   1575
  94.       Width           =   1905
  95.    End
  96.    Begin Menu mnuOptions 
  97.       Caption         =   "&Options"
  98.       Begin Menu mnuOptionsSettings 
  99.          Caption         =   "&Save Settings on Exit"
  100.       End
  101.    End
  102.    Begin Menu mnuExplain 
  103.       Caption         =   "&Explain"
  104.    End
  105. 'Indents, tabulation & spacing in events is purely for readability
  106. Option Explicit                                 'This is optional but can help eliminated runtime variable bugs
  107. Dim lpAppName$, lpKeyName$                      'Dimension variables used. (Must be done if 'Option Explicit' is used)
  108. Dim lpDefault$, lpReturnString$
  109. Dim lpString$, lpFileName$, bufflen%, nSize%
  110. Dim Truncated$, toppos$, leftpos$, WriteString%
  111. Dim c, lpClassName$, IsRunning                  'See function FindWindow% below
  112.                                                 'Declare the API Private read and write functions
  113. Declare Function GetPrivateProfileString% Lib "Kernel" (ByVal lpAppName$, ByVal lpKeyName$, ByVal lpDefault$, ByVal lpReturnString$, ByVal nSize%, ByVal lpFileName$)
  114. Declare Function WritePrivateProfileString% Lib "Kernel" (ByVal lpAppName$, ByVal lpKeyName$, ByVal lpString$, ByVal lpFileName$)
  115.                                                 'The following function can tell you if an application
  116.                                                 'is already running.  In this instance it will look for Windows Notepad
  117.                                                 'See the mnuExplain_Click event
  118. Declare Function FindWindow% Lib "user" (ByVal lpClassName As Any, ByVal lpCaption As Any)
  119. Sub cmdExplain_Click ()
  120.     mnuExplain_Click
  121. End Sub
  122. Sub cmdQuit_Click ()
  123.     If mnuOptionsSettings.Checked = True Then           'If 'Save Settings' ticked then
  124.         toppos$ = Str$(frmini.Top)                      'Convert property values to strings.
  125.         leftpos$ = Str$(frmini.Left)                    '(Unfortunately this adds a leading space)
  126.         lpAppName$ = "windowpos"                         'Re-assign ini.ini AppName
  127.         lpKeyName$ = "Top"                              'Assign ini.ini key name
  128.         lpString$ = Right$(toppos$, Len(toppos$) - 1)   'and strip leftmost space from parameter string
  129.                                                         'Write new value to ini.ini
  130.         WriteString% = WritePrivateProfileString(lpAppName$, lpKeyName$, lpString$, lpFileName$)
  131.         lpKeyName$ = "Left"                             'Assign ini.ini key name
  132.         lpString$ = Right$(leftpos$, Len(leftpos$) - 1) 'and strip leftmost space from parameter string
  133.                                                         'Write new value to ini.ini
  134.         WriteString% = WritePrivateProfileString(lpAppName$, lpKeyName$, lpString$, lpFileName$)
  135.         
  136.         lpAppName$ = "options"                           'Identify next ini.ini section
  137.         lpKeyName$ = "Checked"                           'Assign ini.ini key name
  138.         lpString$ = "0"                                  'Save Settings is 'ticked'
  139.                                                          
  140.                                                          'Write new value to ini.ini
  141.         WriteString% = WritePrivateProfileString(lpAppName$, lpKeyName$, lpString$, lpFileName$)
  142.     Else                                                 'Else Save Settings is not 'ticked'
  143.         lpAppName$ = "options"                           'Identify ini.ini section
  144.         lpKeyName$ = "Checked"                           'Assign ini.ini key name
  145.         lpString$ = "1"                                  'Save Settings is not 'ticked'
  146.                                                          
  147.                                                          'Write new value to ini.ini
  148.         WriteString% = WritePrivateProfileString(lpAppName$, lpKeyName$, lpString$, lpFileName$)
  149.     End If
  150.     End
  151. End Sub
  152. Sub Form_Load ()
  153.     'For demo purposes the code is quite concise and no code optimisation has been attempted
  154.     lpFileName$ = App.Path & "\ini.ini"           'Identify location and filename of ini.ini
  155.     lpAppName$ = "windowpos"                      'Identify section in ini.ini (e.g. heading enclosed in [] )
  156.     lpDefault$ = "0"                              'Set a default for the value on the right of = if it is not found
  157.     lpReturnString$ = Space$(10)                  'Give the returned string a arbitrary length value
  158.     nSize% = Len(lpReturnString$)                 'Set returned string buffer to this length
  159.     lpKeyName$ = "Top"                            'Define key name on left of =
  160.     'Call the read function placing the returned string length into bufflen%
  161.     bufflen% = GetPrivateProfileString(lpAppName$, lpKeyName$, lpDefault$, lpReturnString$, nSize%, lpFileName$)
  162.     Truncated$ = Left$(lpReturnString$, bufflen%) 'Strip unwanted space from right of returned string together with null character
  163.     Label1.Caption = "Window Top: " & Truncated$  'Show Top value in Label1.Caption (For demo purposes only)
  164.     frmini.Top = Val(Truncated$)                  'Set frmini.Top property
  165.     lpKeyName$ = "Left"                           'Redefine key name value on left of =
  166.     'Call the read function placing the returned string length into bufflen%
  167.     bufflen% = GetPrivateProfileString(lpAppName$, lpKeyName$, lpDefault$, lpReturnString$, nSize%, lpFileName$)
  168.     Truncated$ = Left$(lpReturnString$, bufflen%) 'Strip unwanted spaces from right of returned string together with null character
  169.     Label2.Caption = "Window Left: " & Truncated$ 'Show Left value in Label2.Caption (For demo purposes only)
  170.     frmini.Left = Val(Truncated$)                 'Set frmini.Left property
  171.     lpAppName$ = "options"                        'Identify section in ini.ini
  172.     lpKeyName$ = "Checked"                        'Redefine key name value on left of =
  173.     'lpDefault$ = "0"                             'This doesn't change
  174.     'Call the read function placing the returned string length into bufflen%
  175.     bufflen% = GetPrivateProfileString(lpAppName$, lpKeyName$, lpDefault$, lpReturnString$, nSize%, lpFileName$)
  176.     Truncated$ = Left$(lpReturnString$, bufflen%) 'Strip unwanted spaces from right of returned string together with null character
  177.                                                   
  178.     If Truncated$ = "0" Then                      'Toggle 'checked' status of menu item
  179.         mnuOptionsSettings.Checked = True
  180.     Else
  181.         mnuOptionsSettings.Checked = False
  182.     End If
  183. End Sub
  184. Sub mnuExplain_Click ()
  185.     'Check for instance of Windows Notepad
  186.     lpClassName$ = "Notepad"                  'Identify Application to be checked for instance of
  187.     IsRunning = FindWindow(lpClassName$, 0&)  'Call API function
  188.         
  189.     If IsRunning <> 0 Then                    'Application is already running
  190.         MsgBox "Notepad is already running !", 64, ".INI File Read/Write" 'Tell user
  191.         Exit Sub                              'and exit
  192.     Else                                      'Else run it and load the ini.txt file
  193.         c = Shell("Notepad.exe " & App.Path & "\ini.txt", 3) '3 = Window maximised
  194.         SendKeys "%+(EW)", True               'Set WordWrap to on (Command word "True"
  195.                                               'ensures that keys sent are processed fully before
  196.     End If                                    'control is returned to this sub procedure
  197.                                               'False = control returns immediately keys are sent)
  198. End Sub
  199. Sub mnuOptionsSettings_Click ()
  200.     'Is 'Save Settings On Exit' ticked ?
  201.     If mnuOptionsSettings.Checked = True Then 'Toggle checked status
  202.         mnuOptionsSettings.Checked = False
  203.     Else
  204.         mnuOptionsSettings.Checked = True
  205.     End If
  206. End Sub
  207. Sub Timer1_Timer ()
  208.     'The Timer control is included only for the purposes of the demo
  209.     'Ensure correct values are displayed when window has been dragged
  210.     Label1.Caption = "Window Top = " & frmini.Top
  211.     Label2.Caption = "Window Left = " & frmini.Left
  212.     'Repeats every second (i.e Timer1.Interval = 1000)
  213. End Sub
  214.